Imports Costanti
Imports System.Configuration.ConfigurationManager

Partial Class Admin_UserManagement
    Inherits System.Web.UI.Page

    Private _data_user As DS_user
    'Private _data_usr_x_qry As DS_usr_x_qry
    Private _data_querylist As DS_querylist

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        _data_user = Session.Item("DS_user")
        If Not Page.IsPostBack Then
            Session.Item("DS_querylist") = CType(Session.Item("DBA"), DBAccess).ReadDS_queryList
            'LoadQueryListUser()
            Session.Item("QueryListEdited") = 0
        End If
        _data_querylist = Session.Item("DS_querylist")

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim dr_usr As DS_user.TBL_userRow
        Dim dv_usr_x_qry As Data.DataView

        If Session.Item("USR_selected") > -1 Then
            dr_usr = _data_user.TBL_user.FindByUSR_codice(Session.Item("USR_selected"))

            TXB_cognome.Text = dr_usr.USR_cognome
            TXB_nome.Text = dr_usr.USR_nome
            TXB_email.Text = dr_usr.USR_email
            TXB_userid.Text = dr_usr.USR_userid
            RDB_si.Checked = dr_usr.USR_abilitato
            RDB_no.Checked = Not dr_usr.USR_abilitato
            DDL_ruolo.SelectedValue = dr_usr.USR_cod_ruolo
        End If
        PNL_query.Visible = DDL_ruolo.SelectedValue > 1
        If PNL_query.Visible Then
            dv_usr_x_qry = New Data.DataView(_data_user.TBL_usr_x_qry)

            dv_usr_x_qry.RowFilter = String.Concat("UXQ_cod_usr = ", Session.Item("USR_selected"))
            GridView1.DataSource = dv_usr_x_qry
            If Session.Item("QueryListEdited") = 1 Then
                GridView1.EditIndex = 0
            End If
            GridView1.DataBind()
            If GridView1.EditIndex = 0 Then
                Dim ddl As DropDownList
                ddl = GridView1.Rows(0).FindControl("DDL_query_abil")
                ddl.Items.Insert(0, "--- Seleziona una query")
                ddl.SelectedValue = CType(_data_user.TBL_usr_x_qry.Rows(0), DS_user.TBL_usr_x_qryRow).UXQ_cod_qry
            End If
            BTN_abilita_query.Visible = GridView1.EditIndex = -1
            BTN_disabilita_query.Visible = dv_usr_x_qry.Count > 0 And GridView1.EditIndex = -1
        End If

    End Sub

    Protected Sub BTN_salva_dati_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_salva_dati.Click
        Dim dr_usr As DS_user.TBL_userRow

        If Session.Item("USR_selected") = -1 Then
            dr_usr = _data_user.TBL_user.NewRow
            dr_usr.USR_password = AppSettings.Get("DefaultPassword")
            dr_usr.USR_def_pwd = True
            _data_user.TBL_user.Rows.Add(dr_usr)
        Else
            dr_usr = _data_user.TBL_user.FindByUSR_codice(Session.Item("USR_selected"))
        End If
        dr_usr.USR_cognome = TXB_cognome.Text
        dr_usr.USR_nome = TXB_nome.Text
        dr_usr.USR_email = TXB_email.Text
        dr_usr.USR_userid = TXB_userid.Text
        dr_usr.USR_abilitato = RDB_si.Checked
        dr_usr.USR_cod_ruolo = DDL_ruolo.SelectedValue
        dr_usr.AcceptChanges()
        CType(Session.Item("DBA"), DBAccess).WriteDS_User(_data_user)
        '_data_user.WriteXml(Server.MapPath(AppSettings.Get("TBLUserList")))
        Session.Item("DS_user") = _data_user
        Session.Item("USR_selected") = dr_usr.USR_codice

    End Sub

    Protected Sub LNK_elenco_utenti_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LNK_elenco_utenti.Click

        Response.Redirect("UserList.aspx")

    End Sub

    Protected Sub BTN_abilita_query_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_abilita_query.Click
        Dim dr_uxq As DS_user.TBL_usr_x_qryRow

        dr_uxq = _data_user.TBL_usr_x_qry.NewRow
        dr_uxq.UXQ_cod_usr = Session.Item("USR_selected")
        dr_uxq.UXQ_cod_qry = 0
        _data_user.TBL_usr_x_qry.Rows.InsertAt(dr_uxq, 0)
        Session.Item("DS_user") = _data_user
        Session.Item("QueryListEdited") = 1

    End Sub

    Public Function GetQueryName(ByVal nQuery As Integer) As String
        Dim cRtn As String = ""

        If nQuery > 0 Then
            cRtn = _data_querylist.TBL_querylist.FindByQRY_codice(nQuery).QRY_nome
        End If

        Return cRtn

    End Function

    Public Function GetQueryTable() As Data.DataView
        Dim dv_qrl As Data.DataView

        dv_qrl = New Data.DataView(_data_querylist.TBL_querylist)
        dv_qrl.Sort = "QRY_nome"
        Return dv_qrl

    End Function

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit

        _data_user.TBL_usr_x_qry.Rows(0).RejectChanges()
        GridView1.EditIndex = -1
        Session.Item("DS_user") = _data_user
        Session.Item("QueryListEdited") = 0

    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim dr_uxq As DS_user.TBL_usr_x_qryRow

        dr_uxq = _data_user.TBL_usr_x_qry.Rows(0)
        Dim ddl As DropDownList
        ddl = GridView1.Rows(0).FindControl("DDL_query_abil")
        dr_uxq.UXQ_cod_qry = ddl.SelectedValue
        _data_user.AcceptChanges()
        CType(Session.Item("DBA"), DBAccess).WriteDS_User(_data_user)


        '_data_user.WriteXml(Server.MapPath(AppSettings.Get("TBLUserList")))
        GridView1.EditIndex = -1
        'Session.Item("")
        'Session.Item("DS_usr_x_qry") = _data_usr_x_qry
        Session.Item("DS_user") = _data_user
        Session.Item("QueryListEdited") = 0

    End Sub

    Protected Sub BTN_disabilita_query_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_disabilita_query.Click
        Dim lDeleted As Boolean = False
        Dim dr_uxq As DS_user.TBL_usr_x_qryRow

        For Each Grv_Row As GridViewRow In GridView1.Rows
            If CType(Grv_Row.FindControl("CHB_select"), CheckBox).Checked Then
                dr_uxq = _data_user.TBL_usr_x_qry.FindByUXQ_codice(GridView1.DataKeys(Grv_Row.RowIndex).Value)
                dr_uxq.Delete()
                lDeleted = True
            End If
        Next
        If lDeleted Then
            _data_user.TBL_usr_x_qry.AcceptChanges()
            _data_user.WriteXml(Server.MapPath(AppSettings.Get("TBLUserList")))
        End If
        'Session.Item("DS_usr_x_qry") = _data_usr_x_qry
        Session.Item("DS_user") = _data_user

    End Sub

End Class
